package com.itany.controller;


import com.github.pagehelper.PageInfo;
import com.itany.constant.ResponseConstant;
import com.itany.exception.RequestParamsException;
import com.itany.pojo.Permission;
import com.itany.service.PermissionService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService;


    /**
     * 查找所有权限
     *
     * @param page 页码数
     * @param rows 每页数据条数
     * @return 分页权限信息
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer rows,
                                       String parentnameKey, String premissionKey) {
        Map<String, Object> map = new HashMap<>();
        PageInfo<Permission> info;
        info = permissionService.findPermissionAll(page, rows, parentnameKey, premissionKey);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }

    @RequestMapping("/findPermissiontree")
    @ResponseBody
    public List<Map<String, Object>> findPermissiontree() {
        List<Map<String, Object>> tree = new ArrayList<>();
        List<Permission> allRootPermissions = permissionService.findAllRootPermissions();
        for (Permission permission : allRootPermissions) {
            Map<String, Object> node = new HashMap<>();
            node.put("id", permission.getId());
            node.put("text", permission.getName());
            node.put("state", "closed");
            List<Map<String, Object>> childNodes = new ArrayList<>();
            List<Permission> permissionsByParent = permissionService.findPermissionsByParent(permission.getId());
            for (Permission childPermission : permissionsByParent) {
                Map<String, Object> childNode = new HashMap<>();
                childNode.put("id", childPermission.getId());
                childNode.put("text", childPermission.getName());
                childNodes.add(childNode);
            }
            node.put("children",childNodes);
            tree.add(node);
        }
        return tree;
    }


    @RequestMapping("/findRootPermissions")
    @ResponseBody
    public List<Permission> findRootPermissions() {
        List<Permission> allRootPermissions = null;
        try {
            allRootPermissions = permissionService.findAllRootPermissions();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allRootPermissions;
    }

    @RequestMapping("/modifyPermission")
    @ResponseBody
    public ActionResult modifyPermission(String id, String name, String sname, String parentid) {
        ActionResult result = new ActionResult();
        try {
            permissionService.modifyPermission(id, name, sname, parentid);
            result.setMsg("修改成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }
}
